package com.office.web.controller.system;

import java.util.List;
import java.util.Map;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.office.common.annotation.Log;
import com.office.common.core.controller.BaseController;
import com.office.common.core.domain.AjaxResult;
import com.office.common.enums.BusinessType;
import com.office.system.domain.FinanceExpense;
import com.office.system.service.IFinanceExpenseService;
import com.office.common.utils.poi.ExcelUtil;
import com.office.common.core.page.TableDataInfo;

/**
 * 支出记录Controller
 * 
 * @author 数字牧马人
 */
@RestController
@RequestMapping("/system/finance/expense")
public class FinanceExpenseController extends BaseController
{
    @Autowired
    private IFinanceExpenseService financeExpenseService;

    /**
     * 查询支出记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:finance:expense:list')")
    @GetMapping("/list")
    public TableDataInfo list(FinanceExpense financeExpense)
    {
        startPage();
        List<FinanceExpense> list = financeExpenseService.selectFinanceExpenseList(financeExpense);
        return getDataTable(list);
    }

    /**
     * 导出支出记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:finance:expense:export')")
    @Log(title = "支出记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FinanceExpense financeExpense)
    {
        List<FinanceExpense> list = financeExpenseService.selectFinanceExpenseList(financeExpense);
        ExcelUtil<FinanceExpense> util = new ExcelUtil<FinanceExpense>(FinanceExpense.class);
        util.exportExcel(response, list, "支出记录数据");
    }

    /**
     * 获取支出记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:finance:expense:query')")
    @GetMapping(value = "/{expenseId}")
    public AjaxResult getInfo(@PathVariable("expenseId") Long expenseId)
    {
        return success(financeExpenseService.selectFinanceExpenseByExpenseId(expenseId));
    }

    /**
     * 新增支出记录
     */
    @PreAuthorize("@ss.hasPermi('system:finance:expense:add')")
    @Log(title = "支出记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FinanceExpense financeExpense)
    {
        return toAjax(financeExpenseService.insertFinanceExpense(financeExpense));
    }

    /**
     * 修改支出记录
     */
    @PreAuthorize("@ss.hasPermi('system:finance:expense:edit')")
    @Log(title = "支出记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FinanceExpense financeExpense)
    {
        return toAjax(financeExpenseService.updateFinanceExpense(financeExpense));
    }

    /**
     * 删除支出记录
     */
    @PreAuthorize("@ss.hasPermi('system:finance:expense:remove')")
    @Log(title = "支出记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{expenseIds}")
    public AjaxResult remove(@PathVariable Long[] expenseIds)
    {
        return toAjax(financeExpenseService.deleteFinanceExpenseByExpenseIds(expenseIds));
    }

    /**
     * 审批支出记录
     */
    @PreAuthorize("@ss.hasPermi('system:finance:expense:approve')")
    @Log(title = "支出审批", businessType = BusinessType.UPDATE)
    @PostMapping("/approve")
    public AjaxResult approve(@RequestBody Map<String, Object> params)
    {
        Long expenseId = Long.valueOf(params.get("expenseId").toString());
        String status = params.get("status").toString();
        String approver = getUsername(); // 获取当前登录用户作为审批人
        return toAjax(financeExpenseService.approveExpense(expenseId, status, approver));
    }

    /**
     * 统计支出金额
     */
    @PreAuthorize("@ss.hasPermi('system:finance:expense:query')")
    @GetMapping("/amount")
    public AjaxResult getAmount(FinanceExpense financeExpense)
    {
        return success(financeExpenseService.selectExpenseAmount(financeExpense));
    }

    /**
     * 按分类统计支出
     */
    @PreAuthorize("@ss.hasPermi('system:finance:expense:query')")
    @GetMapping("/category")
    public AjaxResult getCategory(FinanceExpense financeExpense)
    {
        List<Map<String, Object>> list = financeExpenseService.selectExpenseByCategory(financeExpense);
        return success(list);
    }
} 